#include "StdAfx.h"
#include "CxControlIO.h"


CxControlIO::CxControlIO():m_port(NULL)
{
}

CxControlIO::~CxControlIO()
{
	if(m_port!=NULL)
		delete m_port;
}

bool				CxControlIO::InitPort(string comstr)
{
	m_port=new CxSerialPort(comstr);
	if(m_port->isOpen())
		return true;
	else 
		return false;
}

bool				CxControlIO::ReleasePort()
{
	if(m_port!=NULL  && m_port->isOpen())
	{
		delete m_port;
		return true;
	}
	else 
		return false;
	
}
void				CxControlIO::fullScreen()
{
	const char msg[]="fullscreen\n";
	
	if(m_port!=NULL)
		m_port->async_write((unsigned char *)msg,strlen(msg),[](bool){});

}
void				CxControlIO::exitFullscreen()
{
	const char msg[]="exitfullscreen\n";
	if(m_port!=NULL)
		m_port->async_write((unsigned char *)msg,strlen(msg),[](bool){});
}
void				CxControlIO::setLight(int  number, bool  isred)
{
	if(isred)
	{
		char msg[BUFSIZ];
		sprintf(msg,"red %d\n",number);
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)msg,strlen(msg),[](bool){});
	}
	else 
	{
		char msg[BUFSIZ];
		sprintf(msg,"green %d\n",number);
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)msg,strlen(msg),[](bool){});
	}
}
void				CxControlIO::switchTo(int  page)
{
	switch(page)
	{
	case 0:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"tiaoshi\n",strlen("tiaoshi\n"),[](bool){});
		break;
	case 1:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"wenmiao\n",strlen("wenmiao\n"),[](bool){});
		break;
	case 2:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"jianmiao\n",strlen("jianmiao\n"),[](bool){});
		break;
	case 3:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"zhencha\n",strlen("zhencha\n"),[](bool){});
		break;
	case 4:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"zhidao\n",strlen("zhidao\n"),[](bool){});
		break;
	case 5:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"daohang1\n",strlen("daohang1\n"),[](bool){});
		break;
	case 6:
		if(m_port!=NULL)
			m_port->async_write((unsigned char *)"daohang2\n",strlen("daohang2\n"),[](bool){});
		break;
	default:
		break;
	}
}